package vn.edu.saigontech.ncms.web.system;

import java.util.Collection;

import vn.edu.saigontech.ncms.model.info.SurveyInfo;
import vn.edu.saigontech.ncms.service.SurveyInfoService;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class SurveyInfoAction extends BaseAction {
	private Long id;
	private String name;
	private String description;
	private boolean active;

	public String list() {
		Collection<SurveyInfo> surveyInfos = SurveyInfoService.INSTANCE.findAll();
		REQUEST().setAttribute("surveyInfos", surveyInfos);
		return SUCCESS;
	}
	
	public String showAdd() {
		return SUCCESS;
	}
	
	public String add() {
		SurveyInfo surveyInfo = new SurveyInfo();
		surveyInfo.setName(this.name);
		surveyInfo.setDescription(this.description);
		surveyInfo.setActive(true);
		surveyInfo = SurveyInfoService.INSTANCE.persist(surveyInfo);
		if(Checker.isNotNull(surveyInfo)) {
			return SUCCESS;
		}
		return ERROR;
	}
	
	public String showEdit() {
		SurveyInfo surveyInfo = SurveyInfoService.INSTANCE.findById(this.id);
		REQUEST().setAttribute("surveyInfo", surveyInfo);
		return SUCCESS;
	}
	
	public String edit() {
		SurveyInfo surveyInfo = SurveyInfoService.INSTANCE.findById(this.id);
		surveyInfo.setName(this.name);
		surveyInfo.setDescription(this.description);
		surveyInfo.setActive(this.active);
		if(Checker.isNotNull(surveyInfo)) {
			return SUCCESS;
		}
		return ERROR;
	}
	
	public String delete() {
		if(SurveyInfoService.INSTANCE.deleteById(this.id)) {
			return SUCCESS;
		}
		return ERROR;
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}
	
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
}